package com.cskaoyan.market.util;

import jdk.internal.org.objectweb.asm.tree.TryCatchBlockNode;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.context.annotation.Bean;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: VoUtil
 * @Auther: lyz
 * @Date: 2023/05/20/21:21
 * @Description:
 */
public class VoUtil {
    public static <T> T objectVo(Object source, Class<T> target) {
        if (source == null) {
            return null;
        }
        T targetObject = null;
        try {
            targetObject = target.newInstance();
            BeanUtils.copyProperties(targetObject, source);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return targetObject;
    }

    public static <T> List<T> objectToVolist(List<?> sourceList, Class<T> target) {
        if (sourceList == null) {
            return null;
        }
        List<T> targetList = new ArrayList<>(sourceList.size());
        for (Object source : sourceList) {
            T targetObject = null;
            try {
                targetObject = target.newInstance();
                BeanUtils.copyProperties(targetObject, source);
                targetList.add(targetObject);

            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return targetList;
    }
}

